function createNewUser() {
    var name = $("#name").val();
    var login = $("#login").val();
    var password = $("#password").val();
    var retypePassword = $("#retypePassword").val();

    if (name == null || name.length == 0) {
        $("#message").html("The field \"name\" is required");
        $("#message").show();
        return;
    }
    if (login == null || login.length == 0) {
        $("#message").html("The field \"login\" is required");
        $("#message").show();
        return;
    }
    if (password == null || password.length == 0) {
        $("#message").html("The field \"password\" is required");
        $("#message").show();
        return;
    }
    if (retypePassword == null || retypePassword.length == 0) {
        $("#message").html("The field \"retypePassword\" is required");
        $("#message").show();
        return;
    }

    if (password != retypePassword) {
        $("#message").html("The fields \"password\" and \"retypePassword\" do not match");
        $("#message").show();
        return;
    }

    dataString = $("#userInsert").serialize();
    $.ajax({
        type: "post",
        url: "UserInsert",
        data: dataString,
        dataType: "xml",
        success: function (data, textStatus, jqXHR) {
            var success = $(data).find("success").text();
            var message = $(data).find("message").text();
            $("#message").html(message);
            if (success == "true") {
                $("#message").html($("#message").html() + "<br/>");
                $("#message").html($("#message").html() + "<a href=\"index.jsp\">Click here to sign in</a>");
            }
            $("#message").show();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $("#message").html(textStatus);
            $("#message").show();
        },
        beforeSend: function (jqXHR, settings) {
            $('#save').attr("disabled", true);
        },
        complete: function (jqXHR, textStatus) {
            $('#save').attr("disabled", false);
        }
    });
}

$(document).ready(function () {
    $("#save").click(function () {
        createNewUser();
    });
    $("#message").hide();
});